package store.lunangangster.engine.impl.entity.mgr;

import java.util.List;
import store.lunangangster.engine.ProcessEngineConfiguration;
import store.lunangangster.engine.impl.entity.IdentityLinkEntity;
import store.lunangangster.engine.impl.entity.IdentityLinkEntityImpl;
import store.lunangangster.engine.impl.entity.IdentityLinkType;
import store.lunangangster.engine.impl.entity.TaskEntity;

public class IdentityLinkEntityManagerImpl extends AbstractEntityManagerImpl<IdentityLinkEntity> implements
    IdentityLinkEntityManager {

  public IdentityLinkEntityManagerImpl(
      ProcessEngineConfiguration processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  @Override
  public Class<? extends IdentityLinkEntity> getManagedEntityClass() {
    return IdentityLinkEntityImpl.class;
  }

  @Override
  public IdentityLinkEntity create() {
    return new IdentityLinkEntityImpl();
  }

  @Override
  public void addCandidateUserToTask(TaskEntity taskEntity, String user) {
    IdentityLinkEntity identityLinkEntity = create();
    identityLinkEntity.setTask(taskEntity);
    identityLinkEntity.setExecutionId(taskEntity.getExecutionId());
    identityLinkEntity.setType(IdentityLinkType.CANDIDATE);
    identityLinkEntity.setUser(user);

    insert(identityLinkEntity);
  }

  @Override
  public void addCandidateGroupToTask(TaskEntity taskEntity, String group) {
    IdentityLinkEntity identityLinkEntity = create();
    identityLinkEntity.setTask(taskEntity);
    identityLinkEntity.setExecutionId(taskEntity.getExecutionId());
    identityLinkEntity.setType(IdentityLinkType.CANDIDATE);
    identityLinkEntity.setGroup(group);

    insert(identityLinkEntity);
  }

  @Override
  public List<IdentityLinkEntity> selectIdentityLinkByExecutionId(String executionId) {
    return selectList("selectIdentityLinkByExecutionId", executionId, null);
  }

  @Override
  public List<IdentityLinkEntity> selectIdentityLinkByTaskId(String taskId) {
    return selectList("selectIdentityLinkByTaskId", taskId, null);
  }
}
